﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThanhTu.GNC.Media.KQXS.DataAccess.BL;
using ThanhTu.GNC.Media.KQXS.DataAccess.DO;
using GNCMedia.KQXS.Admin;
using System.Text.RegularExpressions;
using System.Net.Sockets;
using System.IO;
using System.Text;

namespace GNCMedia.KQXS.MenuPage
{
    public partial class Gopy : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region checkValidEmail
        private byte[] BytesFromString(string str)
        {
            return Encoding.ASCII.GetBytes(str);
        }
        private int GetResponseCode(string ResponseString)
        {
            return int.Parse(ResponseString.Substring(0, 3));
        }
        private bool CheckSMTP_GMAIL(string hostName)
        {
            /* Check SMTP*/
            TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com", 25);
            string CRLF = "\r\n";
            byte[] dataBuffer;
            string ResponseString;
            NetworkStream netStream = tClient.GetStream();
            StreamReader reader = new StreamReader(netStream);
            ResponseString = reader.ReadLine();
            /* Perform HELO to SMTP Server and get Response */
            dataBuffer = BytesFromString("HELO KirtanHere" + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            ResponseString = reader.ReadLine();
            dataBuffer = BytesFromString("MAIL FROM:<YourGmailIDHere@gmail.com>" + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            ResponseString = reader.ReadLine();
            /* Read Response of the RCPT TO Message to know from google if it exist or not */
            dataBuffer = BytesFromString("RCPT TO:<" + hostName + ">" + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            ResponseString = reader.ReadLine();
            if (GetResponseCode(ResponseString) == 550)
            {
                return false;
            }


            /* QUITE CONNECTION */
            dataBuffer = BytesFromString("QUITE" + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            tClient.Close();

            return true;
        }

        private bool checkSMTP_YAHOO(string hostnameyahoo)
        {
            TcpClient tClient = new TcpClient("plus.smtp.mail.yahoo.com", 465);
            string CRLF = "\r\n";
            byte[] dataBuffer;
            string ResponseString;
            NetworkStream netStream = tClient.GetStream();
            StreamReader reader = new StreamReader(netStream);
            ResponseString = reader.ReadLine();
            dataBuffer = BytesFromString("HELO " + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            ResponseString = reader.ReadLine();
            dataBuffer = BytesFromString("MAIL FROM:<myemail@yahoo.com>" + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            ResponseString = reader.ReadLine();
            dataBuffer = BytesFromString("RCPT TO:<" + hostnameyahoo.Trim() + ">" + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            ResponseString = reader.ReadLine();
            if (GetResponseCode(ResponseString) == 550)
            {
                return false;
            }
            dataBuffer = BytesFromString("QUITE" + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            tClient.Close();
            return true;
        }


        #endregion
        protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
        {
            try
            {
                CommentBL vComment = new CommentBL();
                CommentDO vcomentDO = new CommentDO();
                RequimenData req = new RequimenData();

                vcomentDO.Email = txt_mail.Text.Trim();
                vcomentDO.Phone = txt_phone_gopy.Text.Trim();
                vcomentDO.Content = txt_gopy.Value;
                string parten = "[a-z0-9\\._]*[a-z0-9_]@[a-z0-9][a-z0-9\\-\\.]*[a-z0-9]\\.[a-z]{2,6}$";
                if (req.checkInfoComment(vcomentDO.Email, vcomentDO.Phone, vcomentDO.Content))
                {
                    if (Regex.IsMatch(txt_mail.Text.Trim(), parten))
                    {
                        if (CheckSMTP_GMAIL(txt_mail.Text.Trim()))
                            {
                                int checkComment = vComment.SaveComment(vcomentDO);
                                if (checkComment > 0)
                                {
                                    Lbthongbao.ForeColor = System.Drawing.Color.Blue;
                                    Lbthongbao.Text = "Cảm ơn bạn đã góp ý với chúng tôi !";
                                    Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('Cảm ơn bạn đã góp ý với chúng tôi !);", true);
                                }
                                else
                                {
                                    Lbthongbao.ForeColor = System.Drawing.Color.Red;
                                    Lbthongbao.Text = "Lỗi góp ý .Xin vui lòng thử lại !";
                                    Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('Lỗi góp ý .Xin vui lòng thử lại');", true);
                                }
                            }
                            else
                            {
                                Lbthongbao.ForeColor = System.Drawing.Color.Red;
                                Lbthongbao.Text = "Email:" + txt_mail.Text + "KHÔNG tồn tại .Xin vui lòng thử lại !";
                            }
                        
                       
                        
                    }
                    else
                    {
                        Lbthongbao.ForeColor = System.Drawing.Color.Red;
                        Lbthongbao.Text = "Email không hợp lệ .Xin vui lòng thử lại !";
                    }
                }
                else
                {
                    Lbthongbao.ForeColor = System.Drawing.Color.Red;
                    Lbthongbao.Text = "Lỗi góp ý .Xin vui lòng nhập đầy đủ thông tin !";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

            }
        }
    }
}